/*============================================================================*/
/*                     		PANTERS_CUCEI	   		                          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* C Source:        ADC.c
* version:         1.0 
* created_by:      Panters  team 
* date_created:    Oct 1 2013 
*=============================================================================*/
/* DESCRIPTION : C source for ADC management                             */
/*============================================================================*/
/* FUNCTION COMMENT : This file describes the C source template according to  */
/* the new software platform                                                  */
/*                                                                            */
/*============================================================================*/
/*                               OBJECT HISTORY                               */
/*============================================================================*/
/*  REVISION |   DATE      |                               |      AUTHOR      */
/*----------------------------------------------------------------------------*/
/*  1.0      | 1/10/2013  |                               | PANTERS_CUCEI    */
/* First version of this source                                               */
/*============================================================================*/


/* Includes */
#include "derivative.h"
#include "SCR/ADC.h"
/* -------- */
/*==================================================*/ 
/* Definition of constants                          */
/*==================================================*/
/* Functions macros, constants, types and datas         */
#define B_8				0x00
#define bus_clock		0x00
#define COCO_NOT        0x00
/*==================================================*/

/**************************************************************
 *  Name                 :	init_adc
 *  Description          :	initialise for adc
 *  Parameters           :  
 *  Return               :
 *  Critical/explanation :  
 **************************************************************/
void init_adc(void)
{
	
	/*Configuracion de pines para ADC*/
	
	SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK  | SIM_SCGC5_PORTD_MASK; //Clock para el PB y PTD
	
	PORTB_PCR2=PORT_PCR_MUX(0);		//pin multipexeado para adc    ADC0_SE12   potenciometro
	PORTB_PCR3=PORT_PCR_MUX(0);		//pin multipexeado para adc	   ADC0_SE13	potenciometro
	
	PORTD_PCR5 = PORT_PCR_MUX(0); //Pin multiplexeado para salida analoga camara
	PORTD_PCR6 = PORT_PCR_MUX(0); //pin multiplexado para salida analoga camara
	
	
	/*Configuracion del adc*/
	
	SIM_SCGC6 |= (SIM_SCGC6_ADC0_MASK); //Habilitar adc0
	
	/******************CFG1***************************************/
	ADC0_CFG1|=ADC_CFG1_ADICLK(bus_clock);	//ADC Busclock
	ADC0_CFG1|=ADC_CFG1_MODE(B_8);	//resolution 8bits
	
	

	
	/*******************CFG2**************************************/
	
	ADC0_CFG2|=ADC_CFG2_ADLSTS(3); //2extras  ADCK Cycles

	/*****************Conversion por software*********************/
	//ADC0_SC2=0x00;
	
		

}


/**************************************************************
 *  Name                 :	pot value
 *  Description          :	convertion for pot on shield
 *  Parameters           :  number of pot (1 or 2)
 *  Return               :	value data converted by adc
 *  Critical/explanation :  
 **************************************************************/
char pot_value(char pot)
{
	char value=0;
	
	ADC0_CFG2  &= ~(ADC_CFG2_MUXSEL_MASK); //mux A
	
	switch(pot)
	{
	case 2:
		ADC0_SC1A= ADC_SC1_ADCH(12);
		while((ADC0_SC1A & ADC_SC1_COCO_MASK ) == (COCO_NOT));	//Checar que la conversion finalize
		value=ADC0_RA;
		break;
		
	case 1:
		ADC0_SC1A= ADC_SC1_ADCH(13);
		while((ADC0_SC1A & ADC_SC1_COCO_MASK ) == (COCO_NOT));	//Checar que la conversion finalize
		value=ADC0_RA;
		break;
		
	default:
		break;
	}	
				
	
	return(value);
	
}

/**************************************************************
 *  Name                 :	camera_value
 *  Description          :	get adc value for camera AO.
 *  Parameters           :  number of cam (1 or 2)
 *  Return               :	value of adc convertion
 *  Critical/explanation :  
 **************************************************************/

char camera_value(char cam)
{
	ADC0_CFG2  |= ADC_CFG2_MUXSEL_MASK;	//configuracion B
	
	char value=0;
	
	switch (cam)
	{
		case 1:
			ADC0_SC1A= ADC_SC1_ADCH(6);
			while((ADC0_SC1A & ADC_SC1_COCO_MASK ) == (COCO_NOT));	//Checar que la conversion finalize
			value=ADC0_RA;			
			break;
			
		case 2:
			ADC0_SC1A= ADC_SC1_ADCH(7);
			while((ADC0_SC1A & ADC_SC1_COCO_MASK ) == (COCO_NOT));	//Checar que la conversion finalize
			value=ADC0_RA;			
			break;
			
		default:
			break;	
	}
	
	return(value);
	
}

